import http from '@/common/http';
import { Dialog } from 'mand-mobile';
import Loading from '@/components/loading';
const defaultState = () => {
  return {
    orderInfo: null,
    payWXmentInfo: null,
    couponInfo: null,
    BalanceInfo: null,
    PayPwdInfo: null,
    balancePayOrder: null,
    h5wxPaymentInfo: null,
    shareOrderInfo: null
  };
};
const state = defaultState();

const actions = {
  // 只用来通过接口或者异步处理
  getOrderInfo(context, orderId) {
    return new Promise((resolve, reject) => {
      http
        .post('pakj/order/getOrderInfoByID', {
          order_no: orderId
        })
        .then(res => {
          if (res.data.code === '000000') {
            context.commit('setOrderInfo', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  updateOrderInfo(context, params) {
    return new Promise((resolve, reject) => {
      http.post('pakj/order/updateOrder', params).then(res => {
        if (res.data.code === '000000') {
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  getDefaultCoupon(context, orderId) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/coupon/getDefaultCoupon', {
          order_no: orderId
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            context.commit('setCouponInfo', res.data.data);
            resolve();
          } else if (res.data.code === '404404') {
            context.commit('setCouponInfo', res.data.msg);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  getBalancePayAmt(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/getBalancePayAmt', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setBalanceInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  createBalancePay(context, param) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/passenger/validatePayPwd', param).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('validatePayPwdInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200029') {
          reject(new Error(res.data.code + ':' + res.data.msg));
        } else if (res.data.code === '200028') {
          reject(new Error(res.data.code + ':' + res.data.msg));
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  getbalancePayOrder(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/balancePayOrder', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setbalancePayOrder', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          reject(new Error(res.data.code + ':' + res.data.msg));
        } else if (res.data.code === '200006') {
          reject(new Error(res.data.code + ':' + res.data.msg));
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  WXsubmitPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/toPayOrder', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('WXsetPaymentInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  miniprogramPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/toPayOrderMP', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          resolve(res.data.data.JSPParam);
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  h5WxPayment(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/toPayOrderH5', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setH5wxPaymentInfo', res.data.data);
          resolve();
        } else if (res.data.code === '200008') {
          Dialog.alert({
            title: '错误',
            content: '请不要重复支付',
            onConfirm: () => {
              reject(new Error('200008'));
            }
          });
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  // 微信支付成功后 查询订单支付状态
  paymentStatus(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/userorder/payResultQuery', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          if (res.data.data.code === '1') {
            resolve();
          } else if (res.data.data.code === '2') {
            // 订单未支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '5') {
            // 等待支付
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          } else if (res.data.data.code === '4') {
            // 支付失败
            reject(new Error(res.data.data.code + ':' + res.data.data.msg));
          }
        }
      });
    });
  },
  checkPaymentStatus(context, orderno) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/order/orderQueryPayStatus', {
          order_no: orderno
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '1') {
            resolve();
          } else {
            reject(new Error('failed'));
          }
        });
    });
  },
  getShareOrderInfo(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/pakj/order/getShareOrderInfo', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setShareOrderInfo', res.data.data);
          resolve();
        } else {
          reject(new Error(res.data.msg));
        }
      });
    });
  },
  getShareTaxiOrderInfo(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/pakj/order/getShareOrderInfo', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setShareOrderInfo', res.data.data);
          resolve();
        } else {
          reject(new Error(res.data.msg));
        }
      });
    });
  }
};
const mutations = {
  // 修改state的数据。同步操作
  resetOrder(state) {
    Object.assign(state, defaultState());
  },
  setOrderInfo(state, data) {
    console.log('得到的订单信息：', data);
    state.orderInfo = data;
  },
  setCouponInfo(state, data) {
    console.log('获取到优惠券信息：', data);
    state.couponInfo = data;
  },
  setBalanceInfo(state, data) {
    console.log('获取到余额信息：', data);
    state.BalanceInfo = data;
  },
  validatePayPwdInfo(state, data) {
    console.log('验证支付密码是否正确：', data);
    state.PayPwdInfo = data;
  },
  setbalancePayOrder(state, data) {
    console.log('余额支付', data);
    state.balancePayOrder = data;
  },
  WXsetPaymentInfo(state, data) {
    state.payWXmentInfo = data;
  },
  setH5wxPaymentInfo(state, data) {
    state.h5wxPaymentInfo = data;
  },
  setShareOrderInfo(state, data) {
    state.shareOrderInfo = data;
  },
  setShareOrderInfoPath(state, path) {
    state.shareOrderInfo.path = path;
  }
};
const getters = {
  getOrderInfoData(state) {
    return state.orderInfo;
  },
  getcouponInfoData(state) {
    return state.couponInfo;
  },
  getBalanceInfoData(state) {
    return state.BalanceInfo;
  },
  getOrderId(state) {
    if (state.orderInfo) {
      return state.orderInfo.order_no;
    }
    return undefined;
  },
  getWXPaymentInfoData(state) {
    return state.payWXmentInfo;
  },
  validatePayPwdInfoData(state) {
    return state.PayPwdInfo;
  },
  getbalancePayOrderData(state) {
    return state.balancePayOrde;
  },
  getH5wxPaymentInfo(state) {
    return state.h5wxPaymentInfo;
  },
  getShareOrderInfo(state) {
    return state.shareOrderInfo;
  }
};
export default {
  state,
  actions,
  mutations,
  getters
};
